package com.example.first.thread;

import java.util.concurrent.TimeUnit;

/**
 * @Author LSQ
 * @Description Runnable是接口，Thread是实现类
 * @date 2021/5/19 9:12
 * @Version 1.0
 */


public class LearnRunnable2 {

    public static void main(String[] args) {

        // 可读性更强的写法
        Runnable runnable1 = () -> {
            System.out.println("Hello, I am runnable1!");
            System.out.println("Runnable1, my thread name is: " + Thread.currentThread().getName());
        };


        Runnable runnable2 = () -> {
            System.out.println("Hello, I am runnable2!");
            System.out.println("Runnable1, my thread name is: " + Thread.currentThread().getName());
        };

        // lambda表达式可以简单到 {} 都不需要
        Runnable runnable3 = () -> System.out.println("Hello, I am runnable3!");

        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        Thread thread3 = new Thread(runnable3);

        // start会产生一个除了main方法之外的线程，run只有一个main方法

        thread1.start();
        // thread1.run();
        thread2.start();
        thread2.setName("My-thread-2");
        thread3.start();

        // 如果没有 Lambda 表达式，那么原来的代码可能如下
        Runnable runnable4 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello, I am runnable4!");
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread thread4 = new Thread(runnable4);
        System.out.println("print thread4 stauts: " + thread4.getState());
        thread4.start();
        System.out.println("print thread4 stauts: " + thread4.getState());
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("print thread4 stauts: " + thread4.getState());

        }


        // Runnable myTask = new Thread();



    }

}
